package com.wang.springbootinit.controller.admin;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wang.springbootinit.common.BaseResponse;
import com.wang.springbootinit.common.ErrorCode;
import com.wang.springbootinit.common.ResultUtils;
import com.wang.springbootinit.model.entity.Student;
import com.wang.springbootinit.model.param.StudentAddParam;
import com.wang.springbootinit.service.StudentService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 后台，学生管理
 */
@RestController
@RequestMapping("/admin/student")
public class AdminStudentController {
    @Resource
    private StudentService studentService;

    /**
     * 分页查询学生列表
     * 支持按学号、姓名、班级、专业、性别进行模糊搜索
     *
     * @param current 当前页码
     * @param pageSize 每页显示条数
     * @param studentNumber 学号（可选）
     * @param name 姓名（可选）
     * @param className 班级（可选）
     * @param major 专业（可选）
     * @param gender 性别（可选）
     * @return 分页查询结果
     */
    @GetMapping("/list")
    public BaseResponse<Page<Student>> listStudents(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String studentNumber,
            @RequestParam(required = false) String name,
            @RequestParam(required = false) String className,
            @RequestParam(required = false) String major,
            @RequestParam(required = false) Integer gender) {
        Page<Student> studentPage = studentService.listStudents(current, pageSize, studentNumber, name, className, major, gender);
        return ResultUtils.success(studentPage);
    }

    /**
     * 添加学生信息
     *
     * @param param 学生信息
     * @return 新增学生的ID
     */
    @PostMapping("/add")
    public BaseResponse<Long> addStudent(@RequestBody StudentAddParam param) {
        if (param == null) {
            return ResultUtils.error(ErrorCode.PARAMS_ERROR);
        }
        Long studentId = studentService.addStudent(param);
        return ResultUtils.success(studentId);
    }

    /**
     * 根据ID获取学生信息
     *
     * @param studentId 学生ID
     * @return 学生详细信息
     */
    @GetMapping("/get")
    public BaseResponse<Student> getStudent(@RequestParam Long studentId) {
        if (studentId <= 0) {
            return ResultUtils.error(ErrorCode.PARAMS_ERROR);
        }
        Student student = studentService.getStudentById(studentId);
        if (student == null) {
            return ResultUtils.error(ErrorCode.NOT_FOUND_ERROR);
        }
        return ResultUtils.success(student);
    }

    /**
     * 更新学生信息
     *
     * @param student 更新的学生信息
     * @return 更新是否成功
     */
    @PostMapping("/update")
    public BaseResponse<Boolean> updateStudent(@RequestBody Student student) {
        if (student == null) {
            return ResultUtils.error(ErrorCode.PARAMS_ERROR);
        }
        boolean result = studentService.updateStudent(student);
        return ResultUtils.success(result);
    }

    /**
     * 删除学生信息
     *
     * @param id 学生ID
     * @return 删除是否成功
     */
    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteStudent(@RequestParam Long id) {
        if (id <= 0) {
            return ResultUtils.error(ErrorCode.PARAMS_ERROR);
        }
        boolean result = studentService.deleteStudent(id);
        return ResultUtils.success(result);
    }

    /**
     * 获取个人信息
     */
    @GetMapping("/profile")
    public BaseResponse<Student> getProfile(HttpServletRequest request) {
        Student student = studentService.getLoginStudentInfo(request);
        return ResultUtils.success(student);
    }

    /**
     * 更新个人信息
     */
    @PostMapping("/profile/update")
    public BaseResponse<Boolean> updateProfile(@RequestBody Student student, HttpServletRequest request) {
        if (student == null) {
            return ResultUtils.error(ErrorCode.PARAMS_ERROR);
        }
        boolean result = studentService.updateStudentInfo(student, request);
        return ResultUtils.success(result);
    }

    /**
     * 获取学生成绩单
     */
    @GetMapping("/scores")
    public BaseResponse<Map<String, Object>> getStudentScores(HttpServletRequest request) {
        Map<String, Object> scoreInfo = studentService.getStudentScores(request);
        return ResultUtils.success(scoreInfo);
    }

    /**
     * 导出学生信息
     */
    @GetMapping("/export")
    public void exportStudents(HttpServletResponse response) {
        studentService.exportStudents(response);
    }

    /**
     * 导入学生信息
     */
    @PostMapping("/import")
    public BaseResponse<Boolean> importStudents(@RequestParam("file") MultipartFile file) {
        boolean result = studentService.importStudents(file);
        return ResultUtils.success(result);
    }

    /**
     * 获取学生成绩分析
     */
    @GetMapping("/score/analysis")
    public BaseResponse<Map<String, Object>> getScoreAnalysis(HttpServletRequest request) {
        Map<String, Object> analysis = studentService.analyzeStudentScores(request);
        return ResultUtils.success(analysis);
    }

    /**
     * 获取学生排名
     */
    @GetMapping("/ranking")
    public BaseResponse<Map<String, Object>> getStudentRanking(HttpServletRequest request) {
        Map<String, Object> ranking = studentService.getStudentRanking(request);
        return ResultUtils.success(ranking);
    }



    /**
     * 按班级统计学生人数
     * 统计每个班级的学生总数，按班级名称排序
     *
     * @return 班级统计信息列表，包含班级名称和学生数量
     */
    @GetMapping("/stats/class")
    public BaseResponse<List<Map<String, Object>>> getClassStatistics() {
        List<Map<String, Object>> statistics = studentService.getClassStatistics();
        return ResultUtils.success(statistics);
    }

    /**
     * 按专业统计学生人数
     * 统计每个专业的学生总数，按专业名称排序
     *
     * @return 专业统计信息列表，包含专业名称和学生数量
     */
    @GetMapping("/stats/major")
    public BaseResponse<List<Map<String, Object>>> getMajorStatistics() {
        List<Map<String, Object>> statistics = studentService.getMajorStatistics();
        return ResultUtils.success(statistics);
    }
}
